var markerlng,markerlat;
function check_add(){
	if($(".gallery_select").val() != 0){
		if(!markerlng){
			if(confirm("您没有更改地点，确认上传？")){				
				var re = 1;
			}
			if(re==1){
				var center = map.getCenter();
				G("city_lng").value=center.lng;
				G("city_lat").value=center.lat;				
				}else{
					return false;
					}
		}else{
			G("city_lng").value=markerlng;
			G("city_lat").value=markerlat;		
			}
	}else{message("请选择或新建相册");return false;}
}
	function G(id) {
 	 return document.getElementById(id);
	}

	var map = new BMap.Map("container");
	var gc = new BMap.Geocoder();
	if(G("city_lng").value==''){
		var point = new BMap.Point(108.3964,32.9093);
	}else{
		var point = new BMap.Point(G("city_lng").value,G("city_lat").value);
		}
	map.centerAndZoom(point,5);
	map.enableScrollWheelZoom();
	map.addControl(new BMap.NavigationControl());
	var marker = new BMap.Marker(point);  // 创建标注
	map.addOverlay(marker);
	marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的标注
	gc.getLocation(point, function(rs){
  	var addComp = rs.addressComponents;
  	var add= new String;
  	add = ""+addComp.province +  addComp.city +  addComp.district + addComp.street + addComp.streetNumber+"";					
		G("address").value=add;
 		});
	
	var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
	    {"input" : "suggestId",
	    	"location" : map
	});
	
	ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
			var str = "";
	    var _value = e.fromitem.value;
	    var value = "";
	    if (e.fromitem.index > -1) {
	        value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
	    }    
	    str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
	    
	    value = "";
	    if (e.toitem.index > -1) {
	        _value = e.toitem.value;
	        value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
	    }    
	    str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
	    //G("searchResultPanel").innerHTML = str;
	});
	
	var myValue;
	ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
	var _value = e.item.value;
	    myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
	    //G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
	    
	    setPlace();
	});
	
	//初始化地图位置
	$.ajax({url:"/index.class.php",data:{request:1},async:false,success:function(re){
		myValue = re;
	}});
	setPlace();
	
	function setPlace(){
	    map.clearOverlays();    //清除地图上所有覆盖物
	    function myFun(){
	        var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
	        map.centerAndZoom(pp, 14);
	        //var pts= new BMap.Point(center.lng,center.lat);
					gc.getLocation(pp, function(rs){
      			var addComp = rs.addressComponents;
      			var add= new String;
      			add = ""+addComp.province +  addComp.city +  addComp.district + addComp.street + addComp.streetNumber+"";					
						G("address").value=add;
 					 });
 					var marker = new BMap.Marker(pp);
	        map.addOverlay(marker);    //添加标注
	        marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的标注
	        markerlng = marker.point.lng;
	   		  markerlat = marker.point.lat;
	    }
	    var local = new BMap.LocalSearch(map, { //智能搜索
	      onSearchComplete: myFun
	    });
	    local.search(myValue);
	}
	
	var contextMenu = new BMap.ContextMenu();
	var txtMenuItem = [
	  {
	   text:'上传到此地点',
	   callback:function(p){
	   	map.clearOverlays();    //清除地图上所有覆盖物
	    var marker = new BMap.Marker(p), px = map.pointToPixel(p);
	    map.addOverlay(marker);
	    marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的标注
	    markerlng = marker.point.lng;
	    markerlat = marker.point.lat;
	    var pts= new BMap.Point(markerlng,markerlat);
			gc.getLocation(pts, function(rs){
      	var addComp = rs.addressComponents;
      	var add= new String;
      	add = ""+addComp.province +  addComp.city +  addComp.district + addComp.street + addComp.streetNumber+"";					
				G("address").value=add;
 			 });
	   }
	  }
	 ];
	
	 for(var i=0; i < txtMenuItem.length; i++){
	  contextMenu.addItem(new BMap.MenuItem(txtMenuItem[i].text,txtMenuItem[i].callback,100));
	  if(i==1 || i==3) {
	   contextMenu.addSeparator();
	  }
	 }
	 
//	 map.centerAndZoom(point,15);
map.addContextMenu(contextMenu);

//新建相册
$("#new_gallery").live('click',function(){
	$(".temp_gallery").hide();
	$(".new_gallery").show();
});

$("#create_gallery").live('click',function(){
	if($("#gallery_name").val() != ""){
		$.ajax({url:"post_temp.class.php",type:"POST",data:{request:1,gallery_name:$("#gallery_name").val()},success:function(re){
			$(".temp_gallery").empty().html(re+'<a href="#" id="new_gallery">新建相册</a>');
			$(".new_gallery").hide();
			$(".temp_gallery").show();	
		}});
	}
	return false;
});

$("#no_new").live('click',function(){
	$(".temp_gallery").show();
	$(".new_gallery").hide();
});